﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Text.RegularExpressions;
using System.IO;

namespace com.qbengo.utility.nodepicker
{
    public partial class NodePickOverview : Form
    {
        private string pictureName;//图片的路径
        private Bitmap curBitmap; //图片的位图图像
        private Point pictureOrigin = new Point(200, 20);//图片原点，图片的左上角
        private const int pictureLength = 1024; //图片的长
        private const int pictureWidth = 1024;  //图片的宽
        private List<NodeGraphic> nodeList = new List<NodeGraphic>(); //存储选择的点的list
        private bool respond = true;
        private int xOffset;
        private int yOffset;

        private int mapID = 0;   //map的ID
        public NodePickOverview(int mapID)
        {
            InitializeComponent();
            this.mapID = mapID;
        }
        /// <summary>
        /// 由用户找到图片的路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void selectPictureButton_Click(object sender, EventArgs e)
        {
            //创建OpenFileDialg
            OpenFileDialog opnDlg = new OpenFileDialog();

            //为图像选择一个筛选器
            opnDlg.Filter = "all picture file|*.bmp;*.pcx;*.png;*.jpg;*.gif;" +
                "*.tif;*.ico;*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf|" +
                "bitmap(*.bmp;*.jpg;*.png;...)|*.bmp;*.pcx;*.png;*.jpg;*.gif;*.tif;*.ico|" +
                "vectogram(*.wmf;*.eps;*.emf;...)|*.dxf;*.cgm;*.cdr;*.wmf;*.eps;*.emf";
            //设置对话框标题
            opnDlg.Title = "打开图像文件";
            //启用”帮助“按钮
            opnDlg.ShowHelp = true;

            //如果结果为“打开”，选定文件
            if (opnDlg.ShowDialog() == DialogResult.OK)
            {
                //读取当前选中的文件名
                pictureName = opnDlg.FileName;
                //使用Image.FromFile创建图像对象
                try
                {
                    curBitmap = (Bitmap)Image.FromFile(pictureName);
                }
                catch (Exception exp)
                {
                    //抛出异常
                    MessageBox.Show(exp.Message);
                }



                nodeDataGridView.Rows.Clear();
               


                Invalidate();
            }
        }
        /// <summary>
        /// 绘制图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //获取Graphics对象
            Graphics g = e.Graphics;

            if (curBitmap != null)
            {
                //使用DrawImage 方法绘制图像
                //160,20：显示在主窗体内，图像左上角的坐标
                //curBitmap.Width,curBitmap.Height:图像的宽度和高度
                g.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y); //应用平移变换
                g.DrawImage(curBitmap, pictureOrigin.X, pictureOrigin.Y, pictureWidth, pictureLength);
            }

            Pen redPen = new Pen(Brushes.Red, 2); //宽度为1的红色画笔
            foreach (NodeGraphic node in nodeList)
            {
                Point point = NormalCoordinateToWorld(node);
                g.DrawEllipse(redPen, point.X - 3, point.Y - 3, 6, 6);//画出边长为6的正方形所包围的圆。

                g.DrawString(node.ID.ToString(),
                new Font("Bold", 10), Brushes.Chocolate, new PointF(point.X, point.Y - 15));
            }
            Pen blackPen = new Pen(Brushes.Black, 2); //宽度为2的黑色画笔


        }
        /// <summary>
        /// 鼠标按下在图片上选点，将选择的点保存在pointList和pointCheckedListBox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (curBitmap == null)
                return;

            NodeGraphic node;
            xOffset = this.AutoScrollPosition.X;
            yOffset = this.AutoScrollPosition.Y;
            int x = e.X - xOffset;   //全局坐标
            int y = e.Y - yOffset;   //全局坐标

            if (x >= pictureOrigin.X && x <= pictureOrigin.X + pictureWidth &&
            y >= pictureOrigin.Y && y <= pictureOrigin.Y + pictureLength)
            {
                node = new NodeGraphic(mapID * 100000  + nodeList.Count,
                    WorldCoordinateNormalise(x, y));

                //Pen curPen = new Pen(Brushes.Red, 2); //宽度为1的黑色画笔
                Pen curPen = new Pen(Brushes.Chocolate, 2); //宽度为1的黑色画笔
                Graphics g = this.CreateGraphics();

                g.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y); //应用平移变换
                g.DrawEllipse(curPen, x - 3, y - 3, 6, 6);//画出边长为6的正方形所包围的圆。

                g.DrawString((mapID * 100000  + nodeList.Count).ToString(),
                    new Font("Bold", 10), Brushes.Black,
                    new PointF(x, y - 15));

                nodeList.Add(node);

                nodeDataGridView.Rows.Add();  //增加一行

                respond = false;
                nodeDataGridView[0, nodeList.Count - 1].Value = node.ID;
                nodeDataGridView[1, nodeList.Count - 1].Value = node.X;
                nodeDataGridView[2, nodeList.Count - 1].Value = node.Y;
                respond = true;
            }
        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 画出所有选择的点并且在图中标记
        /// </summary>
        private void PaintAllNodes()
        {
            Pen curPen = new Pen(Brushes.Red, 1); //宽度为1的红色画笔
            Graphics g = this.CreateGraphics();
            int pixelNum;
            foreach (NodeGraphic node in nodeList)
            {
                pixelNum = (node.ID.ToString().Length / 2) * 3;

                Point point = NormalCoordinateToWorld(node);
                g.DrawEllipse(curPen, point.X - 2, point.Y - 2, 4, 4);//画出边长为4的正方形所包围的圆。

                g.DrawString(node.ID.ToString(),
                new Font("New Timer", 8), Brushes.Black, new PointF(point.X - pixelNum, point.Y - 13));
            }
        }

        /// <summary>
        /// 将世界坐标转换为单位坐标
        /// </summary>
        /// <param name="x">X坐标</param>
        /// <param name="y">Y坐标</param>
        /// <returns></returns>
        private PointF WorldCoordinateNormalise(int x, int y)
        {
            PointF point;
            point = new PointF((float)(x - pictureOrigin.X) / pictureWidth, //point=new PointF((float)(x-pictureOrigin.X+1) / pictureWidth,     
            (float)(y - pictureOrigin.Y) / pictureLength);//    (float)(y-pictureOrigin.Y+1) / pictureLength);

            return point;
        }

        /// <summary>
        /// 将单位坐标转换为世界坐标
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private Point NormalCoordinateToWorld(NodeGraphic node)
        {
            Point point;
            point = new Point((int)(node.X * pictureWidth) + pictureOrigin.X,
            (int)(node.Y * pictureLength) + pictureOrigin.Y);

            return point;
        }
        /// <summary>
        /// 滑动滚动条时，重绘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Scroll(object sender, ScrollEventArgs e)
        {
            //Invalidate();
        }
        /// <summary>
        /// 数据导出到文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exportButton_Click(object sender, EventArgs e)
        {
            String exportFileName;

            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter = "text file|*.txt;*doc";

            saveFileDialog.RestoreDirectory = true; //记忆上次打开的目录

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                exportFileName = saveFileDialog.FileName.ToString(); //获得文件路径
                StreamWriter streamWriter = new StreamWriter(exportFileName);

                streamWriter.WriteLine(pictureName);       //图片的本地路径
                streamWriter.WriteLine(mapID);             //map_id
                
                streamWriter.WriteLine(nodeList.Count);    //node的总数
                

                foreach (NodeGraphic node in nodeList)    //node的信息
                {
                    streamWriter.WriteLine(node.ID + "\t" + node.X + "\t" + node.Y);
                }

  

                streamWriter.Flush();
                streamWriter.Close();

            }
        }

    }
}
